package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.anno.PermissionName;
import cn.wolfcode.education.domain.RegularStu;
import cn.wolfcode.education.domain.SystemDictionaryItem;
import cn.wolfcode.education.query.RegularStuChartQuery;
import cn.wolfcode.education.query.RegularStuQuery;
import cn.wolfcode.education.service.IRegularStuService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("regularStu")
public class RegularStuController {
    @Autowired
    private IRegularStuService regularStuService;

    @RequestMapping("")
    @PermissionName("正式学员管理")
    @RequiresPermissions("regularStu:view")
    public String index() {
        return "regularStu/regularStu";
    }

    @RequestMapping("query")
    @ResponseBody
    @PermissionName("正式学员列表")
    @RequiresPermissions("regularStu:query")
    public PageResult query(RegularStuQuery qo) {
        return regularStuService.query(qo);
    }

    @RequestMapping("update")
    @ResponseBody
    @PermissionName("正式学员更新")
    @RequiresPermissions("regularStu:update")
    public JSONResult update(RegularStu regularStu) {
        JSONResult jsonResult = new JSONResult();
        try {
            regularStuService.update(regularStu);
        } catch (Exception e) {
            jsonResult.setMsg("更新失败");
        }
        return jsonResult;
    }

    @RequestMapping("removeStu")
    @ResponseBody
    @PermissionName("正式学员退学")
    @RequiresPermissions("regularStu:removeStu")
    public JSONResult removeStu(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            regularStuService.removeStu(id);
        } catch (Exception e) {
            jsonResult.setMsg("退学失败");
        }
        return jsonResult;
    }

    @RequestMapping("restStu")
    @ResponseBody
    @PermissionName("正式学员休学")
    @RequiresPermissions("regularStu:restStu")
    public JSONResult restStu(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            regularStuService.restStu(id);
        } catch (Exception e) {
            jsonResult.setMsg("休学失败");
        }
        return jsonResult;
    }

    @RequestMapping("recoverStu")
    @ResponseBody
    @PermissionName("正式学员恢复")
    @RequiresPermissions("regularStu:recoverStu")
    public JSONResult recoverStu(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            regularStuService.recoverStu(id);
        } catch (Exception e) {
            jsonResult.setMsg("恢复失败");
        }
        return jsonResult;
    }

    @RequestMapping("promote")
    @ResponseBody
    @PermissionName("正式学员升班")
    @RequiresPermissions("regularStu:promote")
    public JSONResult promote(Long id, int status) {
        JSONResult jsonResult = new JSONResult();
        try {
            regularStuService.promote(id, status);
        } catch (Exception e) {
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

    @RequestMapping("endRegularStu")
    @ResponseBody
    @PermissionName("正式学员毕业")
    @RequiresPermissions("regularStu:endRegularStu")
    public JSONResult endRegularStu(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            regularStuService.endRegularStu(id);
        } catch (Exception e) {
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

    @RequestMapping("retentionRegularStu")
    @ResponseBody
    @PermissionName("正式学员留级")
    @RequiresPermissions("regularStu:retentionRegularStu")
    public JSONResult retentionRegularStu(Long id, Long classgradeId) {
        JSONResult jsonResult = new JSONResult();
        try {
            regularStuService.retentionRegularStu(id, classgradeId);
        } catch (Exception e) {
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

    //查询数据字典中所有校区的信息
    @RequestMapping("selectCampusBySystemDictionary")
    @ResponseBody
    public List<SystemDictionaryItem> selectCampusBySystemDictionary() {
        return regularStuService.selectAllCampusBySystemDictionary();
    }

    //查询数据字典中所有学院的信息
    @RequestMapping("selectCollegeBySystemDictionary")
    @ResponseBody
    public List<SystemDictionaryItem> selectCollegeBySystemDictionary() {
        return regularStuService.selectAllCollegeBySystemDictionary();
    }

    //查询数据字典中所有支付类型的信息
    @RequestMapping("selectAllPaymentType")
    @ResponseBody
    public List<SystemDictionaryItem> selectAllPaymentType() {
        return regularStuService.selectAllPaymentType();
    }

    //查询数据字典中所有付款方式的信息
    @RequestMapping("selectAllPayment")
    @ResponseBody
    public List<SystemDictionaryItem> selectAllPayment() {
        return regularStuService.selectAllPayment();
    }

    //正式学员报表页面定位
    @RequestMapping("/chart")
    @PermissionName("正式学员报表")
    @RequiresPermissions("regularStu:chart")
    public String chart(Model model) {
        model.addAttribute("groupByTypes", RegularStuChartQuery.groupByTypes);
        return "regularStu/regularStuChart";
    }

    //查询正式学员报表
    @RequestMapping("/chart/selectRegularStuChart")
    @ResponseBody
    public List<Map<String, Object>> selectRegularStuChart(RegularStuChartQuery qo) {
        return regularStuService.queryForRegularStuChart(qo);
    }

    // 查询某个学员是否存在
    @RequestMapping("/isExsists")
    @ResponseBody
    public JSONResult isExsists(String stuName) {
        JSONResult result = new JSONResult();
        try {
            regularStuService.isExsists(stuName);
            return result;
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            return result;
        }
    }
}
